package cn.easy.service.impl;

import cn.easy.api.DataService;
import cn.easy.common.pojo.DataViewPojo;
import cn.easy.mapper.DataMapper;
import cn.easy.mapper.DeviceMapper;
import cn.easy.mapper.SensorMapper;
import cn.easy.mapper.UserMapper;
import cn.easy.pojo.*;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class DataServiceImpl implements DataService {
    @Autowired
    private DataMapper dataMapper;
    @Autowired
    private SensorMapper sensorMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 验证用户身份
     *
     * @param sensorId
     * @param userKey
     * @return
     */
    private void checkUser(int sensorId, String userKey) throws Exception {
        int deviceId;
        Sensor sensor = sensorMapper.selectByPrimaryKey(sensorId);
        if (sensor == null) throw new Exception("设备不存在");
        deviceId = sensor.getDeviceId();
        //身份验证
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserKeyEqualTo(userKey);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() == 0) throw new Exception("找不到key对应的用户");//用户不存在
        User user = users.get(0);
        Device device = deviceMapper.selectByPrimaryKey(deviceId);
        if (device == null) throw new Exception("设备不存在");//设备不存在
        if (device.getUserId() != user.getUserId()) throw new Exception("身份验证失败");//身份验证失败
    }



    /**
     * 新增一条数据，需要身份验证
     *
     * @param userKey
     * @param data
     */
    @Override
    public void insertData(String userKey, Data data) throws Exception {
        /*---身份验证----*/
        checkUser(data.getSensorId(), userKey);

        /*-----通过身份验证，增加数据------*/
        data.setDataTime(new Date());
        dataMapper.insert(data);
        /*---------修改最后更新时间-----------*/
        Sensor sensor = sensorMapper.selectByPrimaryKey(data.getSensorId());
        sensor.setSensorUpdatetime(new Date());
        sensorMapper.updateByPrimaryKey(sensor);
    }

    /**
     * 删除一条数据,需要身份验证
     *
     * @param userKey
     * @param dataId
     */
    @Override
    public void deleteData(String userKey, int dataId) throws Exception {
        Data data = dataMapper.selectByPrimaryKey(dataId);
        if (data == null) throw new Exception("没有这条数据记录");
        /*---身份验证----*/
        checkUser(data.getSensorId(), userKey);
        /*-----通过身份验证，删除数据------*/
        dataMapper.deleteByPrimaryKey(dataId);
    }

    /**
     * 获取一个传感器指定时间段内的全部数据,如果传感器所属于的设备是公开的可以不传userKey
     *
     * @param sensorId
     * @param startTime
     * @param endTime
     * @param userKey
     * @return
     */
    @Override
    public List<DataViewPojo> searchSensorDate(int sensorId, Date startTime, Date endTime, String... userKey) throws Exception {
        String key = null;
        /*---------判断有没有userKey,有就取出来-----------*/
        if (userKey.length != 0) key = userKey[0].toString();

        /*---------身份认证------------*/
        Sensor sensor = sensorMapper.selectByPrimaryKey(sensorId);
        if (sensor == null) throw new Exception("找不到传感器");
        Device device = deviceMapper.selectByPrimaryKey(sensor.getDeviceId());
        //判断是不是私人设备，如果是要进行身份认证
        if (device.getDevicePersonal() == 0) {
            if (key == null) throw new Exception("私人设备，需要提供userKey");
            else checkUser(sensorId, key);
        }
        /*----------查询指定时间段内的数据-------------*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start = null;
        String end = null;
        if (startTime != null) start = format.format(startTime);
        if (endTime != null) end = format.format(endTime);
        List<DataViewPojo> list = dataMapper.selectDataWithTime(sensorId, start, end);
        return list;
    }
}
